package com.lagou;

import com.lagou.consumer.ServiceConsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.time.LocalTime;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ConsumerApplication
 * @Description
 * @Author playboy
 * @Date 2021/6/19 1:24 下午
 * @Version 1.0
 **/
public class ConsumerApplication {
	private static Logger log = LoggerFactory.getLogger(ConsumerApplication.class);

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfig.class);
		ServiceConsumer serviceConsumer = context.getBean(ServiceConsumer.class);

		//初始化线程池
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(20, 20, 5000L,
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(20),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.AbortPolicy());

		int minute = LocalTime.now().getMinute();
		Long count = 0L;

		while (true) {
			//对每分钟内发送的请求数做个统计
			if (minute != LocalTime.now().getMinute()) {
				System.out.println(LocalTime.now() + "这一分钟内，累计发送" + count);
				count = 0L;
				minute = LocalTime.now().getMinute();
			} else {
				count++;
			}
			//暂停15毫秒
			try {
				Thread.sleep(15L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			threadPoolExecutor.execute(serviceConsumer);
		}

	}

	@Configuration
	@ComponentScan(basePackages = "com.lagou.consumer")
	@EnableDubbo(scanBasePackages = "com.lagou.consumer")
	@PropertySource("classpath:/dubbo-consumer.properties")
	static class ConsumerConfig {

	}

}
